const app = getApp()
var serverUrl = app.serverUrl;

Page({
    data: {
      bgmList:[],
      serverUrl:"",
      videoParams:{}
  
    },

    onLoad: function (params) {
      var me=this;
      console.log("带过来的参数"+params)
      me.setData({
        videoParams: params
      })
   
      
      //调用后台接口
      wx.request({
        url: serverUrl + '/bgm/list',
        method: "POST",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {

          
          wx.hideLoading();
          if (res.data.status == 200) {
            var bgmList = res.data.data
            me.setData({
              bgmList: bgmList,
              serverUrl: serverUrl
            });

          }

        }
      })
     
    },


    upload:function(e){
      var me=this;
      var serverUrl = app.serverUrl;

      var bgmId=e.detail.value.bgmId;

      var desc = e.detail.value.desc;


      console.log(bgmId + "***" + desc)


      var duration = me.data.videoParams.duration;
      var tmpHeight = me.data.videoParams.tmpHeight;
      var tmpWidth = me.data.videoParams.tmpWidth;
      var tmpVideoUrl = me.data.videoParams.tmpVideoUrl;
      var tmpCoverUrl = me.data.videoParams.tmpCoverUrl;

      wx.showLoading({
        title: '上传中...',
      })
      wx.uploadFile({
        url: serverUrl + '/video/upload',
        formData:{
          userId: app.userInfo.id,
          bgmId: bgmId,
          dec: desc,
          videoSeconds: duration,
          videoHeight: tmpHeight,
          videoWith: tmpWidth
        },
        name: 'file',
        filePath: tmpVideoUrl,
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          wx.hideLoading();
          var data=JSON.parse(res.data);
          console.log(data);
          if (data.status == 200) {
            console.log(200);
            var videoId = data.data;
             wx.showLoading({
              title: '上传中...',
            })
            wx.uploadFile({
              url: serverUrl + '/video/uploadCover',
              formData: {
                userId: app.userInfo.id,
                videoId: videoId
              },
              name: 'file',
              filePath: tmpCoverUrl,
              header: {
                'content-type': 'application/json' // 默认值
              },
              success: function (res) {
                var data = JSON.parse(res.data);
                console.log(data);
                wx.hideLoading();
                if (data.status == 200) {
                  wx.showToast({
                    title: '上传成功!~~',
                    icon: "success"
                  });
                  wx.navigateBack({
                    delta: 1,
                  })
                } else if (data.status == 500) {
                  wx.showToast({
                    title: "失败"
                  })
                }
              }
            })







          } else if (data.status == 500) {
            wx.showToast({
              title: "后台出错"
            })
          }
        }
      })

    }
})

